public class FundingPoolMaintenance {
	private Map<Id,Restricted_Fund_Pool__c> FundingPoolMap = new Map<Id,Restricted_Fund_Pool__c>();
	public List<Restricted_Fund_For_Project__c> ProjectFundingInPools = new List<Restricted_Fund_For_Project__c>();
	
	public FundingPoolMaintenance(Map<Id,Restricted_Fund_Pool__c> TheseFundingPools) {
		//get the passed in map
		FundingPoolMap = TheseFundingPools;
		//system.debug('Map as passed in to HH object: '+HouseholdsMap);
		//create a list of Contacts that are members of that Household
		if(FundingPoolMap.size()>0) {
			ProjectFundingInPools = [Select Id, Amount__c,Restricted_Fund_Pool__c from Restricted_Fund_for_Project__c where Restricted_Fund_Pool__c IN : FundingPoolMap.keySet() order by Restricted_Fund_Pool__c];
		
		}
	}
	
	public void reCalcluateFundPoolBalance() {
		Id PreviousFundPoolId;
        Id CurrentFundPoolId;
        Double FundsAllocatedTotal;
        FundsAllocatedTotal=0;
                
        PreviousFundPoolId=null;
        
        for(Restricted_Fund_Pool__c DrawdownToZeroOut : FundingPoolMap.values()) {
        	DrawdownToZeroOut.Amount_Allocated__c=0;
        }
       //system.debug('invoices: '+InvoicesInProjects);
        for (Restricted_Fund_for_Project__c AllocationsToTotal : ProjectFundingInPools) {
        	Restricted_Fund_Pool__c thisFundPool;
            CurrentFundPoolId = AllocationsToTotal.Restricted_Fund_Pool__c ;            
			thisFundPool = FundingPoolMap.get(CurrentFundPoolId);		
            if(CurrentFundPoolId!=PreviousFundPoolId) {
                //add the map
                if (PreviousFundPoolId!=null) {
                    thisFundPool.Amount_Allocated__c = FundsAllocatedTotal;
                    FundsAllocatedTotal=0;
                    
                }

                //start a new map
               // system.debug('get here? '+ TasksToRollup.Expected_Hours__c);
				if (AllocationsToTotal.Amount__c!=null) {
					FundsAllocatedTotal = AllocationsToTotal.Amount__c;
				}
                PreviousFundPoolId = CurrentFundPoolId;
            } else {
                //add a new element to the map
                if (AllocationsToTotal.Amount__c!=null) {
					FundsAllocatedTotal+= AllocationsToTotal.Amount__c;
                }
                
            }  
            thisFundPool.Amount_Allocated__c = FundsAllocatedTotal;          
                
        }
	}
}